home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
C++
/
Frameworks
/
Sprocket Framework DR2
/
Sprocket Framework
/
ThreadContext.cp
< prev
next >
Wrap
Text File
|
1996-06-15
|
4KB
|
212 lines
/*
File: AEThreads.cp
Project: Sprocket Framework 1.1 (DR2), released 6/15/96
Contains: Routines for installing threaded AppleEvent handlers
To Do: To really support AppleScript™, we’ll need to do ALOT more work in here.
Sprocket Major Contributors:
----------------------------
Dave Falkenburg, producer of Sprocket 1.0
Bill Hayden, producer of Sprocket 1.1
Steve Sisak, producer of the upcoming Sprocket 2.0
Pete Alexander Steve Falkenburg Randy Thelen
Eric Berdahl Nitin Ganatra Chris K. Thomas
Marshall Clow Dave Hershey Leonard Rosenthal
Tim Craycroft Dave Mark Dean Yu
David denBoer Gary Powell
Cameron Esfahani Jon Summers Apple Computer, Inc.
Comments, Additions, or Corrections:
------------------------------------
Bill Hayden, Nikol Software <nikol@codewell.com>
*/
#ifndef _THREADCONTEXT_
#include "ThreadContext.h"
#endif
#include "SprocketMacros.h"
#include <OSUtils.h>
/*****************************************************************************/
TThreadContext::TThreadContext()
{
fThreadID = kNoThreadID;
fThreadResult = nil;
#if !GENERATINGCFM
fSavedA5 = SetCurrentA5();
#endif
#if CW7_EXCEPTIONS
// set up interface to C++ exception runtime support
__new_exception_state(&fExceptionState, fCatchBuffer, sizeof(fCatchBuffer));
#endif
}
/*****************************************************************************/
TThreadContext::~TThreadContext()
{
}
/*****************************************************************************/
void TThreadContext::CreateThread( ThreadStyle threadStyle,
ThreadEntryProcPtr threadEntry,
Size stackSize,
ThreadOptions options)
{
FailOSErr(NewThread(threadStyle, threadEntry, this, stackSize, options, &fThreadResult, &fThreadID));
try
{
#if GENERATING68K && GENERATINGCFM
FailOSErr(SetThreadSwitcher(fThreadID, NewThreadSwitchProc(SwitchInProc), this, true));
FailOSErr(SetThreadSwitcher(fThreadID, NewThreadSwitchProc(SwitchOutProc), this, false));
FailOSErr(SetThreadTerminator(fThreadID, NewThreadTerminationProc(TerminationProc), this));
#else
FailOSErr(SetThreadSwitcher(fThreadID, SwitchInProc, this, true));
FailOSErr(SetThreadSwitcher(fThreadID, SwitchOutProc, this, false));
FailOSErr(SetThreadTerminator(fThreadID, TerminationProc, this));
#endif
}
catch(OSErr err)
{
OSErr ignore = DisposeThread(fThreadID, &fThreadResult, (options & kUsePremadeThread) != 0);
fThreadID = kNoThreadID;
throw(err);
}
}
/*****************************************************************************/
void TThreadContext::SwitchIn(void)
{
#if CW7_EXCEPTIONS
// restore exception-handling context
ExceptionState dummy;
__switch_exception_state(&fExceptionState, &dummy);
#endif
}
/*****************************************************************************/
void TThreadContext::SwitchOut(void)
{
#if CW7_EXCEPTIONS
// save exception-handling context
__switch_exception_state(&fExceptionState, &fExceptionState);
#endif
}
/*****************************************************************************/
void TThreadContext::Terminate(void)
{
if (fThreadID != kNoThreadID)
{
fThreadID = kNoThreadID;
}
}
/*****************************************************************************/
pascal void TThreadContext::SwitchInProc(ThreadID /*threadBeingSwitched*/, void *switchProcParam)
{
TThreadContext* thread = (TThreadContext*) switchProcParam;
#if !GENERATINGCFM
long savedA5 = SetA5(thread->fSavedA5);
#endif
#if THREAD_PROFILE
::ProfilerSwitchToThread(thread->mProfilerRef);
#endif
thread->SwitchIn();
#if !GENERATINGCFM
SetA5(savedA5);
#endif
}
/*****************************************************************************/
pascal void TThreadContext::SwitchOutProc(ThreadID /*threadBeingSwitched*/, void *switchProcParam)
{
TThreadContext* thread = (TThreadContext*) switchProcParam;
#if !GENERATINGCFM
long savedA5 = SetA5(thread->fSavedA5);
#endif
thread->SwitchOut();
#if !GENERATINGCFM
SetA5(savedA5);
#endif
}
/*****************************************************************************/
pascal void TThreadContext::TerminationProc(ThreadID /*threadBeingSwitched*/, void *terminationProcParam)
{
TThreadContext* thread = (TThreadContext*) terminationProcParam;
#if !GENERATINGCFM
long savedA5 = SetA5(thread->fSavedA5);
#endif
thread->Terminate();
#if !GENERATINGCFM
SetA5(savedA5);
#endif
}